import { defineStore } from 'pinia'
import userApi from '@/api/user';
const userSotre = defineStore("user", {
	state: () => {
		return {
			iphone: uni.getStorageSync('iphone')||'',
			openId: uni.getStorageSync('openId')||'',
			userRole:3,
			userinfo:{},
			shopId:'',
			allowMonthSettle:'',//商户是否可以月结
			roleList:[],//拥有的身份列表
			publicUser:false,//公共服务员
		}
	},
	persist: {
		enabled: true,
		H5Storage: window?.localStorage,
		strategies: [
			{
				key: 'user',
				storage: window?.sessionStorage
			}
		]
	},
	actions: {
		setIphone(iphone) {
			uni.setStorageSync('iphone', iphone)
			this.iphone = iphone
		},
		setOpenId(id){
			this.setOpenId = id
		},
		setAllowMonthSettle(val){
			this.allowMonthSettle = val
		},
		inputChange(type,item){
		},
		setUserRole(val){
			this.userRole = val
		},
		setUserinfo(data){
			this.userinfo = data
		},
		setPublicUser(){
			this.publicUser = true
		},
	    setShopId(id){
			this.shopId = id
		},
		setRoleList(){
			userApi.getRoleList().then((res: any)=>{
				this.roleList = res.data
			})
		},
		outLogin(){
			this.userinfo = {}
		}
	},
	getters:{
		/*
	      当前登录身份状态
		  return {number} => 返回一个number  0 未登录 3商户 4 服务员
		*/
		isLoginType(state){
			if(Object.keys(state.userinfo).length===0){
				return 0
			}else{
			    return state.userRole
			}
		},
		isWaiter(state){//是否有服务员身份
			return state.roleList.some((item: any)=> item.role ===4)
		},
		isShop(state){//是否有商户身份
			return state.roleList.some((item: any)=> item.role ===3)
		}
	}
})
export default userSotre
